<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .map {
            position: relative;
            width: 800px;
            height: 600px;
            background-color: #ccc;
        }
    </style>
</head>
<body>
    <div class="map"></div>
    <script>
        // 产生随机数
        (function (window) {
            // 随机数构造函数
            function Random() {};
            function Random() {};
            function Random() {};
            Random.prototype.getRandom(min,max) {
                return Math.floor(Math.random() * (max - min) + min);
            }
            Random.prototype.getRandom = function (min,max) {
                return Math.floor(Math.random() * (max - min) + min);
            }
            Random.prototype.getRandom = function (min,max) {
                return Math.floor(Math.random() * (max - min) + min);
            }
            window.Random = new Random();
            window.Random = new Random();
        })(window)
        // 产生随机食物
       (function (window) {

       
            // 获取地图区域
            var map = document.querySelector(".map");
            var map = document.querySelector('.map');
            var map = document.querySelector('.map');
            // 创建食物构造函数也就是小方块
            function Food(width, height, color) {
                this.width = width || 20;
                this.height = height || 20;
                this.color = color;
                this.x = 0;
                this.y = 0;
                this.element = document.createElement('div');
            }
            function Food(width, height, color) {
                this.width = width || 20;
                this.height = height || 20;
                this.color = color || orange;
                this.x = 0;
                this.y = 0;
                this.element = document.createElement('div');
            }
            function Food(width, height, color) {
                this.width = width || 20;
                this.height = height || 20;
                this.color = color || orange;
                this.x = 0;
                this.y = 0;
                this.element = document.createElement('div');
            }
            // 给小方块添加render方法，渲染
            Food.prototype.render = function (map) {
                var x = Random.getRandom(0, map.offsetWidth / this.width) * this.width;
                var y = Random.getRandom(0,map.offsetHeight / this.height) * this.height;
                this.x = x;
                this.y = y;
                var div = this.element;
                div.style.left = this.x + 'px';
                div.style.top = this.y + 'px';
            }
            Food.prototype.render = function (map) {
                var x = Random.getRandom(0,(map.offsetWidth / this.width) * this.width);
                var y = Random.getRandom(0,(map.offsetHeight / this.height) * this.height);
                this.x = x;
                this.y = y;
                var div =this.element;
                div.style.left = this.x + 'px';
                div.style.top = this.y + 'px';
            }
            Food.prototype.render = function (map) {
                var x = Random.getRandom(0,(map.offsetWidth / this.width) * this.width);
                var y = Random.getRandom(0,(map.offsetHeight / this.height) * this.height);
                this.x = x;
                this.y = y;
                var div = this.element;
                div.style.left = this.x + 'px';
                div.style.top = this.y + 'px';
            }
            Food.prototype.init = function (map) {
                var div = this.element;
                div.style.position = "absolute";
                div.style.width = this.width + 'px';
                div.style.height = this.height + 'px';
                div.style.backgroundColor = this.color;
                map.appendChild(div);
                this.render(map);
            }
            Food.prototype.init = function (map) {
                var div = this.element;
                div.style.position = "absolute";
                div.style.width = this.width + 'px';
                div.style.height = this.height + 'px';
                div.style.backgroundColor = this.color;
                map.appendChild(div);
                this.render(map);
            }
            var food = new Food(20,20,'orange');
            food.init(map);
        })(window)
    </script>
</body>
</html>